import com.peihongli.controller.UserController;
import com.peihongli.spring.Bean1;
import com.peihongli.spring.Bean2;
import com.peihongli.spring.Bean3;
import com.peihongli.spring.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest1 {
    /**
     * 构造方法实例化
     */
    @Test
    public void beanTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
        System.out.println(bean1);
    }

    /**
     * 静态工厂实例化
     */
    @Test
    public void bean2Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
        System.out.println(bean2);
    }

    /**
     * 实例工厂实例化
     */
    @Test
    public void bean3Test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
        Bean3 bean3 = (Bean3) context.getBean("bean3");
        System.out.println(bean3);
    }
    @Test
    public void scopeTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean1 bean1 = (Bean1) context.getBean("bean1");
        Bean1 bean2 = (Bean1) context.getBean("bean1");
        if(bean1 == bean2){
            System.out.println("相等");
        }
    }
    @Test
    public void personTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person.toString());
    }
    @Test
    public void personTest2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person2");
        System.out.println(person.toString());
    }
    @Test
    public void AnnoTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        userController.save();
    }
}
